package com.xframe.xdal.core.model.sqlbuilder;


import com.xframe.xdal.core.constant.DalConstants;
import com.xframe.xdal.core.constant.DataSourceTypeEnums;
import com.xframe.xdal.core.handler.HandlerColumnMapManage;
import com.xframe.xdal.core.util.StringUtil;
import lombok.Data;

import java.util.List;

/**
 * 功能描述:SQL构造器的返回值
 *
 * @author xufeng
 * @version 1.0
 * @date 2022-09-26
 */
@Data
public class SqlExpression {

    /**
     * 数据源类型
     */
    private DataSourceTypeEnums dataSourceTypeEnums;
    /**
     * 主sql语句
     */
    private String innerSql = "";
    /**
     * order by sql 语句
     */
    private String orderBySql = "";
    /**
     * 参数集合
     */
    private List<Object> params;
    /**
     * select 语句映射的数据结构
     */
    HandlerColumnMapManage handlerColumnMapManage;

    /**
     * 输出sql 长度限制
     */
    private static final Integer MAX_SQL_LEN = 500;


    public SqlExpression(String sql,DataSourceTypeEnums dataSourceTypeEnums) {
        this.dataSourceTypeEnums = dataSourceTypeEnums;
        this.innerSql = sql;
        this.params = null;
    }

    public SqlExpression(String sql, List<Object> params,DataSourceTypeEnums dataSourceTypeEnums) {
        this.dataSourceTypeEnums = dataSourceTypeEnums;
        this.innerSql = sql;
        this.params = params;
    }

    public SqlExpression(String sql, String orderBySql, List<Object> params,DataSourceTypeEnums dataSourceTypeEnums) {
        this.dataSourceTypeEnums = dataSourceTypeEnums;
        this.innerSql = sql;
        this.orderBySql = orderBySql;
        this.params = params;
    }

    public SqlExpression(String sql, String orderBySql, List<Object> params, HandlerColumnMapManage handlerColumnMapManage, DataSourceTypeEnums dataSourceTypeEnums) {
        this.dataSourceTypeEnums = dataSourceTypeEnums;
        this.innerSql = sql;
        this.handlerColumnMapManage = handlerColumnMapManage;
        this.orderBySql = orderBySql;
        this.params = params;
    }

    private String getFullSqlHelp() {
        if (this.orderBySql == null || this.orderBySql.equals("")) {
            return this.innerSql;
        }
        return this.innerSql + this.orderBySql;
    }

    public String getFullSql() {

        String SQL = getFullSqlHelp();
//        if(dataSourceTypeEnums == DataSourceTypeEnums.Oracle){
//            return SQL.toUpperCase();
//        }
//        if(dataSourceTypeEnums == DataSourceTypeEnums.MySql){
//            return SQL.toLowerCase();
//        }
        return SQL;
    }

    /**
     * 模板
     */
    private final static String LOG_TEMPLATE = "实际执行的sql语句:  %s";

    /**
     * 输出实际sql 语句
     *
     * @return String
     */
    public String toLogSql() {
        String fullSql = getFullSql();
        if (StringUtil.isBlank(fullSql)) {
            return fullSql;
        }
        fullSql = StringUtil.format(fullSql.replaceAll(DalConstants.SQL_TRAN_PLACE_HOlDER, DalConstants.SQL_BRACE), params.toArray());
        if (fullSql.length() > MAX_SQL_LEN) {
            fullSql = fullSql.substring(0, MAX_SQL_LEN);
        }
        return String.format(LOG_TEMPLATE, fullSql);
    }
}
